import gnu.io.SerialPort;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import javax.swing.JList;
import java.awt.Rectangle;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import java.awt.Dimension;
import java.util.Iterator;
import java.util.TreeSet;

import javax.swing.JLabel;
import javax.swing.JButton;

public class SerialCommSettingDialog extends JDialog  {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JComboBox jComboBoxCom = null;
	private JComboBox jComboBoxBps = null;
	private JComboBox jComboBoxChar = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JComboBox jComboBoxStop = null;
	private JLabel jLabel3 = null;
	private JComboBox jComboBoxFlow = null;
	private JLabel jLabel4 = null;
	private JButton jButtonCancel = null;
	private JButton jButtonOK = null;
	private JComboBox jComboBoxParity = null;
	private JLabel jLabel5 = null;
	public int exitCode=0;
	/**
	 * @param owner
	 */
	public SerialCommSettingDialog(Frame owner) {
		super(owner,true);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(355, 264);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(8, 144, 89, 25));
			jLabel5.setText("Parity");
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(8, 192, 89, 25));
			jLabel4.setText("Flow control");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(8, 112, 89, 25));
			jLabel3.setText("Stop bit");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(8, 80, 89, 25));
			jLabel2.setText("Char bit");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(8, 48, 89, 25));
			jLabel1.setText("Bitrate");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(8, 16, 89, 25));
			jLabel.setText("Port");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJComboBoxCom(), null);
			jContentPane.add(getJComboBoxBps(), null);
			jContentPane.add(getJComboBoxChar(), null);
			jContentPane.add(jLabel, null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(getJComboBoxStop(), null);
			jContentPane.add(jLabel3, null);
			jContentPane.add(getJComboBoxFlow(), null);
			jContentPane.add(jLabel4, null);
			jContentPane.add(getJButtonCancel(), null);
			jContentPane.add(getJButtonOK(), null);
			jContentPane.add(getJComboBoxParity(), null);
			jContentPane.add(jLabel5, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jComboBoxCom	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxCom() {
		if (jComboBoxCom == null) {
			jComboBoxCom = new JComboBox();
			jComboBoxCom.setBounds(new Rectangle(104, 16, 112, 25));
			TreeSet<String> ts = SerialComm.enumSerialPort(1, 10);
			Iterator<String> ite = ts.iterator();
			while(ite.hasNext()){
				jComboBoxCom.addItem(ite.next());
			}
			onChangeCommPortSelection();
			jComboBoxCom.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					//onChangeCommPortSelection();
				}
			});
		}
		return jComboBoxCom;
	}
	private void onChangeCommPortSelection(){
		String port = (String)jComboBoxCom.getSelectedItem();
		if(port.substring(0, 3).equals("COM")){
			SerialPort sp = SerialComm.getSerialPort(port);
			if(sp==null) return;
			setValueJComboBoxBps(sp.getBaudRate());
			setValueJComboBoxChar(sp.getDataBits());
			setValueJComboBoxStop(sp.getStopBits());
			setValueJComboBoxParity(sp.getParity());
			setValueJComboBoxFlow(sp.getFlowControlMode());
		}
	}
	public void setValueJComboBoxCom(String port){
		int idx=0;
		for(idx=0;idx<jComboBoxCom.getItemCount();idx++){
			String str = (String)(jComboBoxCom.getItemAt(idx));
			if(str.compareToIgnoreCase(port)==0){
				getJComboBoxCom().setSelectedIndex(idx);
			}
		}
		
	}
	public String getPort(){
		String ret = (String)getJComboBoxCom().getSelectedItem();
		return ret;
	}
	/**
	 * This method initializes jComboBoxBps	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxBps() {
		if (jComboBoxBps == null) {
			jComboBoxBps = new JComboBox();
			jComboBoxBps.setBounds(new Rectangle(104, 48, 113, 25));
			jComboBoxBps.addItem(110);
			jComboBoxBps.addItem(300);
			jComboBoxBps.addItem(600);
			jComboBoxBps.addItem(1200);
			jComboBoxBps.addItem(2400);
			jComboBoxBps.addItem(4800);
			jComboBoxBps.addItem(9600);
			jComboBoxBps.addItem(14400);
			jComboBoxBps.addItem(19200);
			jComboBoxBps.addItem(38400);
			jComboBoxBps.addItem(57600);
			jComboBoxBps.addItem(115200);
			jComboBoxBps.addItem(230400);
			jComboBoxBps.addItem(460800);
			jComboBoxBps.addItem(921600);
		}
		return jComboBoxBps;
	}
	private void setValueJComboBoxBps(int boud){
		getJComboBoxBps().setSelectedItem(boud);
	}
	public int getBps(){
		return (Integer)(getJComboBoxBps().getSelectedItem());
	}
	/**
	 * This method initializes jComboBoxChar	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxChar() {
		if (jComboBoxChar == null) {
			jComboBoxChar = new JComboBox();
			jComboBoxChar.setBounds(new Rectangle(104, 80, 113, 25));
			jComboBoxChar.addItem(5);
			jComboBoxChar.addItem(6);
			jComboBoxChar.addItem(7);
			jComboBoxChar.addItem(8);
		}
		return jComboBoxChar;
	}
	private void setValueJComboBoxChar(int charwidth){
		getJComboBoxChar().setSelectedItem(charwidth);
	}
	public int getChar(){
		return (Integer)getJComboBoxChar().getSelectedItem();
	}
	/**
	 * This method initializes jComboBoxStop	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxStop() {
		if (jComboBoxStop == null) {
			jComboBoxStop = new JComboBox();
			jComboBoxStop.setBounds(new Rectangle(104, 112, 113, 25));
			jComboBoxStop.addItem("1");
			jComboBoxStop.addItem("1.5");
			jComboBoxStop.addItem("2");
		}
		return jComboBoxStop;
	}
	private void setValueJComboBoxStop(int stopbit){
		int index=1;
		switch(stopbit){
		case SerialPort.STOPBITS_1:	// 1
			index=0; break; 
		case SerialPort.STOPBITS_1_5: // 3
			index=1; break; 
		case SerialPort.STOPBITS_2: // 2
			index=2; break; 
		}
		getJComboBoxStop().setSelectedIndex(index);
	}
	public int getStopbit(){
		int ret = 1;
		switch(getJComboBoxStop().getSelectedIndex()){
		case 0: ret= SerialPort.STOPBITS_1; break;
		case 1: ret= SerialPort.STOPBITS_1_5; break;
		case 2: ret= SerialPort.STOPBITS_2; break;
		}
		return ret;
	}
	/**
	 * This method initializes jComboBoxParity	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxParity() {
		if (jComboBoxParity == null) {
			jComboBoxParity = new JComboBox();
			jComboBoxParity.setBounds(new Rectangle(104, 146, 113, 23));
			jComboBoxParity.addItem("none");
			jComboBoxParity.addItem("ODD");
			jComboBoxParity.addItem("EVEN");
		}
		return jComboBoxParity;
	}
	private void setValueJComboBoxParity(int parity){
		int index=0;
		switch(parity){
		case SerialPort.PARITY_NONE:	// 0
			index=0; break; 
		case SerialPort.PARITY_ODD: // 1
			index=1; break; 
		case SerialPort.PARITY_EVEN: // 2
			index=2; break; 
		}
		getJComboBoxParity().setSelectedIndex(index);
	}
	public int getParity(){
		int ret = 0;
		switch(getJComboBoxParity().getSelectedIndex()){
		case 0: ret= SerialPort.PARITY_NONE; break;
		case 1: ret= SerialPort.PARITY_ODD; break;
		case 2: ret= SerialPort.PARITY_EVEN; break;
		}
		return ret;
	}

	/**
	 * This method initializes jComboBoxFlow	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxFlow() {
		if (jComboBoxFlow == null) {
			jComboBoxFlow = new JComboBox();
			jComboBoxFlow.setBounds(new Rectangle(104, 192, 113, 25));
			jComboBoxFlow.addItem("none");
			jComboBoxFlow.addItem("XON/XOFF");
			jComboBoxFlow.addItem("RTS/CTS");
		}
		return jComboBoxFlow;
	}
	private void setValueJComboBoxFlow(int flow){
		int index=0;
		switch(flow){
		case SerialPort.FLOWCONTROL_NONE:	
			index=0; break; 
		case SerialPort.FLOWCONTROL_XONXOFF_IN | SerialPort.FLOWCONTROL_XONXOFF_OUT: 
			index=1; break; 
		case SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT: 
			index=2; break; 
		}
		getJComboBoxFlow().setSelectedIndex(index);
	}
	public int getFlow(){
		int ret = 0;
		switch(getJComboBoxFlow().getSelectedIndex()){
		case 0: ret= SerialPort.FLOWCONTROL_NONE; break;
		case 1: ret= SerialPort.FLOWCONTROL_XONXOFF_IN | SerialPort.FLOWCONTROL_XONXOFF_OUT; break;
		case 2: ret= SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT; break;
		}
		return ret;
	}
	public String getCommParam(){
		String ret = new String();
		ret = String.format("%s/%d/%d/%d/%d/%d",getPort(),getBps(),getChar(),getStopbit(),getParity(),getFlow());
		return ret;
	}
	public boolean setCommParam(String set){
		if(set==null) return false;
		if(set.isEmpty()) return false;
		String[] tok = set.split("/");
		if(tok.length != 6) return false;
		setValueJComboBoxCom(tok[0]);
		setValueJComboBoxBps(Integer.valueOf(tok[1]));
		setValueJComboBoxChar(Integer.valueOf(tok[2]));
		setValueJComboBoxStop(Integer.valueOf(tok[3]));
		setValueJComboBoxParity(Integer.valueOf(tok[4]));
		setValueJComboBoxFlow(Integer.valueOf(tok[5]));
		return true;
	}
	/**
	 * This method initializes jButtonCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setBounds(new Rectangle(240, 72, 73, 33));
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					exitCode=-1;
					setVisible(false);
								}
			});
		}
		return jButtonCancel;
	}

	/**
	 * This method initializes jButtonOK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setBounds(new Rectangle(240, 16, 73, 33));
			jButtonOK.setText("OK");
			jButtonOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					exitCode=1;
					setVisible(false);
				}
			});
		}
		return jButtonOK;
	}


}  //  @jve:decl-index=0:visual-constraint="10,10"
